﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using nSL.Config;
using System.Data;

namespace nSL.Libs
{
    class nSLLibs
    {
        public static bool Login(string ticket)
        {
            try
            {
                DataSet ds = new DataSet();
                string test = Query.Login(ticket);
                ds = Zyg40.Database.ConnectServer.Retreive(Query.Login(ticket));
                if ((ds != null) && (ds.Tables[0].Rows.Count > 0))
                {
                    Common.USER_DATA = ds;

                    #region Query set course and file
                    DataSet dsCourse = Zyg40.Database.ConnectServer.Retreive(Query.GetCourse(ticket));
                    if ((dsCourse != null) && (dsCourse.Tables[0].Rows.Count > 0))
                        Common.USER_COURSE = dsCourse;

                    DataSet dsFile = Zyg40.Database.ConnectServer.Retreive(Query.GetFile(ticket));
                    if ((dsFile != null) && (dsFile.Tables[0].Rows.Count > 0))
                        Common.COURSE_FILE = dsFile;
                    #endregion

                    return true;
                }
                else
                {
                    Console.WriteLine(Zyg40.Database.ConnectServer.ErrorLog);
                    return false;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }

        public enum MSOffice
        {
            Word,
            Excel,
            Powerpoint
        }

        public static string MSOfficePath(MSOffice productName)
        {
            string _key = "powerpnt.exe";
            switch (productName)
            {
                case MSOffice.Word: _key = "Winword.exe";
                    break;
                case MSOffice.Powerpoint: _key = "powerpnt.exe";
                    break;
                case MSOffice.Excel: _key = "excel.exe";
                    break;
                default: return null;
            }
            const string RegKey = @"Software\Microsoft\Windows\CurrentVersion\App Paths";
            string ValueKey = RegKey + "\\" + _key;
            RegistryKey _mainKey = Registry.LocalMachine;
            try
            {
                _mainKey = _mainKey.OpenSubKey(ValueKey, false);
                if (_mainKey != null)
                {
                    //ValueKey += "\\Path";
                    string value = (string)_mainKey.GetValue("PATH");
                    value += _key;
                    _mainKey.Close();
                    return value.ToString();
                }
                else
                    return null;
            }
            catch
            {
                return null;
            }
        }

        public static bool IsOfficeInstalled()
        {
            const string RegKey = @"Software\Microsoft\Windows\CurrentVersion\App Paths";
            string _key = "powerpnt.exe";
            string ValueKey = RegKey + "\\" + _key;
            RegistryKey _mainKey = Registry.LocalMachine;
            try
            {
                _mainKey = _mainKey.OpenSubKey(ValueKey, false);
                if (_mainKey != null)
                {
                    //ValueKey += "\\Path";
                    var value = _mainKey.GetValue("PATH");
                    _mainKey.Close();
                    return true;
                }
                else
                    return false;
            }
            catch
            {
                return false;
            }
        }

        public static string ProgramFilesx86()
        {
            if (8 == IntPtr.Size
                || (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
            {
                return Environment.GetEnvironmentVariable("ProgramFiles(x86)");
            }

            return Environment.GetEnvironmentVariable("ProgramFiles");
        }
    }
}
